package com.cy.ppj.service.order.impl;

import com.cy.ppj.comm.constant.Constants;
import com.cy.ppj.comm.utils.CommonUtils;
import com.cy.ppj.dao.mapper.customer.CustomerContactMapper;
import com.cy.ppj.dao.mapper.customer.CustomerHouseInfoMapper;
import com.cy.ppj.dao.mapper.customer.CustomerIdCardAuthRecordMapper;
import com.cy.ppj.dao.mapper.customer.CustomerImageMapper;
import com.cy.ppj.dao.mapper.order.OrderGoodsExtraInfoMapper;
import com.cy.ppj.dao.mapper.order.OrderImageMapper;
import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.domian.customer.CustomerHouseInfo;
import com.cy.ppj.model.domian.customer.CustomerIdCardAuthRecord;
import com.cy.ppj.model.enums.customer.CustomerImageType;
import com.cy.ppj.model.query.customer.CustomerAuthRecordQuery;
import com.cy.ppj.model.query.order.JydOrderListAdminQuery;
import com.cy.ppj.model.vo.customer.*;
import com.cy.ppj.model.vo.order.ImageListVO;
import com.cy.ppj.model.vo.order.JydOrderDetailAdminVO;
import com.cy.ppj.model.vo.order.JydOrderListAdminVO;
import com.cy.ppj.model.vo.order.OrderImageListVO;
import com.cy.ppj.service.BaseService;
import com.cy.ppj.service.comm.FileService;
import com.cy.ppj.service.order.JydOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 家易贷订单 service
 * @Author tanyilin
 * @Date 2019/6/27 15:55
 */
@Service
public class JydOrderServiceImpl extends BaseService implements JydOrderService {

    @Autowired
    private OrderGoodsExtraInfoMapper orderGoodsExtraInfoMapper;
    @Autowired
    private CustomerIdCardAuthRecordMapper customerIdCardAuthRecordMapper;
    @Autowired
    private CustomerHouseInfoMapper customerHouseInfoMapper;
    @Autowired
    private CustomerContactMapper customerContactMapper;
    @Autowired
    private OrderImageMapper          orderImageMapper;
    @Autowired
    private CustomerImageMapper customerImageMapper;
    @Autowired
    private FileService               fileService;


    @Override
    public PageBean<JydOrderListAdminVO> selectApplyingJydOrderList(JydOrderListAdminQuery query) {

        int total = orderGoodsExtraInfoMapper.selectApplyingJydOrderListCount(query);
        List<JydOrderListAdminVO> list = new ArrayList<>();
        if(total > 0) {
            query.startPage();
            list = orderGoodsExtraInfoMapper.selectApplyingJydOrderList(query);
            list.stream().forEach(o -> {
                o.setInterestTypeText(o.getInterestType().getText());
            });
        }

        return new PageBean<>(list, total);
    }

    @Override
    public PageBean<JydOrderListAdminVO> selectDecisionPassJydOrderList(JydOrderListAdminQuery query) {

        int total = orderGoodsExtraInfoMapper.selectDecisionPassJydOrderListCount(query);
        List<JydOrderListAdminVO> list = new ArrayList<>();
        if(total > 0) {
            query.startPage();
            list = orderGoodsExtraInfoMapper.selectDecisionPassJydOrderList(query);
            list.stream().forEach(o -> {
                o.setInterestTypeText(o.getInterestType().getText());
            });
        }

        return new PageBean<>(list, total);
    }

    @Override
    public PageBean<JydOrderListAdminVO> selectLoanSuccessJydOrderList(JydOrderListAdminQuery query) {

        int total = orderGoodsExtraInfoMapper.selectLoanSuccessJydOrderListCount(query);
        List<JydOrderListAdminVO> list = new ArrayList<>();
        if(total > 0) {
            query.startPage();
            list = orderGoodsExtraInfoMapper.selectLoanSuccessJydOrderList(query);
            list.stream().forEach(o -> {
                o.setInterestTypeText(o.getInterestType().getText());
            });
        }

        return new PageBean<>(list, total);
    }

    @Override
    public PageBean<JydOrderListAdminVO> selectRejectJydOrderList(JydOrderListAdminQuery query) {

        int total = orderGoodsExtraInfoMapper.selectRejectJydOrderListCount(query);
        List<JydOrderListAdminVO> list = new ArrayList<>();
        if(total > 0) {
            query.startPage();
            list = orderGoodsExtraInfoMapper.selectRejectJydOrderList(query);
            list.stream().forEach(o -> {
                o.setInterestTypeText(o.getInterestType().getText());
            });
        }

        return new PageBean<>(list, total);
    }

    @Override
    public JydOrderDetailAdminVO getOrderDetail(Long orderId) {

        JydOrderDetailAdminVO detail = orderGoodsExtraInfoMapper.selectJydOrderDetailAdmin(orderId);
        state(detail != null, "订单数据异常！");
        Long customerId = detail.getCustomerId();

        //查询客户认证信息
        CustomerAuthDetailVO customerAuthDetailVO = customerIdCardAuthRecordMapper.selectCustomerAuthDetail(customerId);
        state(customerAuthDetailVO != null, "客户信息异常!");
        customerAuthDetailVO.setSexText(customerAuthDetailVO.getSex() == 1 ? "男" : "女");
        customerAuthDetailVO.setAge(CommonUtils.getAgeByIdNo(customerAuthDetailVO.getIdNo()));

        //查询客户房屋信息
        CustomerHouseInfo dbCustomerHouseInfo = new CustomerHouseInfo();
        dbCustomerHouseInfo.setCustomerId(customerId);
        dbCustomerHouseInfo = customerHouseInfoMapper.selectOne(dbCustomerHouseInfo);
        state(dbCustomerHouseInfo != null, "客户房屋信息异常");
        CustomerHouseInfoVO customerHouseInfoVO = new CustomerHouseInfoVO();
        BeanUtils.copyProperties(dbCustomerHouseInfo, customerHouseInfoVO);
        customerHouseInfoVO.setHouseTypeText(customerHouseInfoVO.getHouseType().getText());

        //查询客户联系人信息
        List<CustomerContactInfoVO> contactInfoList = customerContactMapper.selectCustomerContactInfoByCustomerId(customerId);
        contactInfoList.stream().forEach(c -> {
            c.setRelationshipText(c.getRelationship().getText());
        });

        //返回
        detail.setCustomerAuthDetail(customerAuthDetailVO);
        detail.setHouseInfo(customerHouseInfoVO);
        detail.setContactInfoList(contactInfoList);
        return detail;
    }

    @Override
    public List<ImageListVO> getOrderImageList(Long orderId, Long customerId) {

        List<ImageListVO> imageList = new ArrayList<>();
        //查询客户身份证列表
        List<CustomerImageListVO> faceImageList = customerImageMapper.selectCustomerImageListByCustomerId(customerId, CustomerImageType.ID_CARD_FACE);
        for (CustomerImageListVO bean : faceImageList) {
            ImageListVO imageListVO = new ImageListVO();
            imageListVO.setName(bean.getName());
            imageListVO.setSrc(fileService.getFileUrl(bean.getPath(), Constants.MALL_BUCKET_NAME));
            imageList.add(imageListVO);
        }
        List<CustomerImageListVO> backImageList = customerImageMapper.selectCustomerImageListByCustomerId(customerId, CustomerImageType.ID_CARD_BACK);
        for (CustomerImageListVO bean : backImageList) {
            ImageListVO imageListVO = new ImageListVO();
            imageListVO.setName(bean.getName());
            imageListVO.setSrc(fileService.getFileUrl(bean.getPath(), Constants.MALL_BUCKET_NAME));
            imageList.add(imageListVO);
        }

        //查询订单图片资料
        List<OrderImageListVO> orderImageList = orderImageMapper.selectOrderImageListByOrderId(orderId, null);
        for (OrderImageListVO bean : orderImageList) {
            ImageListVO imageListVO = new ImageListVO();
            imageListVO.setName(bean.getName());
            imageListVO.setSrc(fileService.getFileUrl(bean.getPath(), Constants.MALL_BUCKET_NAME));
            imageList.add(imageListVO);
        }

        return imageList;
    }

    @Override
    public PageBean<CustomerAuthRecordVO> selectCustomerAuthRecord(CustomerAuthRecordQuery query) {

        int total = customerIdCardAuthRecordMapper.selectCustomerAuthRecordCount(query);
        List<CustomerAuthRecordVO> list = new ArrayList<>();
        if(total > 0) {
            query.startPage();
            list = customerIdCardAuthRecordMapper.selectCustomerAuthRecord(query);
            list.stream().forEach(c -> {
                c.setAuthStatusText(c.getAuthStatus().getText());
            });
        }

        return new PageBean<>(list, total);
    }

    @Override
    public CustomerAuthDetailVO getCustomerAuthDetail(Long customerId) {

        //查询客户认证信息
        CustomerAuthDetailVO customerAuthDetailVO = customerIdCardAuthRecordMapper.selectCustomerAuthDetail(customerId);
        state(customerAuthDetailVO != null, "客户信息异常!");
        customerAuthDetailVO.setSexText(customerAuthDetailVO.getSex() == 1 ? "男" : "女");
        customerAuthDetailVO.setAge(CommonUtils.getAgeByIdNo(customerAuthDetailVO.getIdNo()));
        customerAuthDetailVO.setAuthStatusText(customerAuthDetailVO.getAuthStatus().getText());

        return customerAuthDetailVO;
    }
}
